define([ 'angular'], function(angular) {
'use strict';
	angular.module('inventory.itemProperSingle', [])

	.controller('itemProperSingleCtrl'
			,['$rootScope', '$scope', 'BizData', '$uibModalInstance','params'
				, 'title', '$uibModal', '$ocLazyLoad', f])//控制器结束
	function f($rootScope, $scope, BizData, $uibModalInstance, params
			, title, $uibModal, $ocLazyLoad) {


		var Model = this;
		
		
		/**
		name:bizData#onNewCreateParam
		description: <b>[回调型事件]</b>业务新增数据创建新增参数事件，可以增加和修改用户自定义的参数
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"param" : {justep.Request.ActionParam} 新增参数对象,
			"defaultValues" : array - 新增行的默认值数组 ，数组的长度决定新增行数，数组中的对象是列的默认值
				defaultValues格式：
					[
						{列名:默认值,...},
						{列名:默认值,...},
						...
					]
		}
		</xmp>	
		@see justep.XData#newData
		@see justep.Request.ActionParam
		*/
		Model.dataMasterNewCreateParam = function(event) {
			//event.param = {"billType": billType};
		};
		
		Model.dataMasterAfterRefresh = function(event) {
			//打开窗口之后，至于编辑状态
			Model.edit();
		};
		

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataMasterBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
		};
		
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataMasterAfterDelete = function(event) {
//			alert('删除之后！')
		};

		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataMasterIndexChanged = function(event) {

		};
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataMasterDataChanged = function(event) {
			
		};
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataMasterValueChanged = function(event) {

		};
		


		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataMasterBeforeNew = function(event) {

		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataMasterAfterNew = function(event){

		}

		Model.dataMasterBeforeSave = function(event) {
//			console.log('dataMasterBeforeSave-->',event);
		};

		Model.dataMasterAfterSave = function(event) {
			//保存成功
			$uibModalInstance.close(0);
		};
		
		

		Model.gridRowClick = function(event){
			
		}

		Model.gridRowDblClick = function(event){
			Model.edit();
		}
		
		
		
		var options = {};
		options.container = Model;
		options.concept = 'ItemProper';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
 		options.creater = '/inventory/itemProper/list/create';
 		options.reader = '/inventory/itemProper/list/query';
		options.writer = '/inventory/itemProper/list/save';
		options.deleter = '/inventory/itemProper/list/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = 'fID';
		options.directDeleteMode = true;
		options.limit = 15;
		
		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataMasterIndexChanged;
		options.onBeforeNew = Model.dataMasterBeforeNew;
		options.onAfterNew = Model.dataMasterAfterNew;
		options.onBeforeDelete = Model.dataMasterBeforeDelete;
		options.onAfterDelete = Model.dataMasterAfterDelete;
		options.onValueChanged = Model.dataMasterValueChanged;
		options.onDataChanged = Model.dataMasterDataChanged;
		options.onAfterRefresh = Model.dataMasterAfterRefresh;
		options.onBeforeSave = Model.dataMasterBeforeSave;
		options.onAfterSave = Model.dataMasterAfterSave;
		options.onNewCreateParam = Model.dataMasterNewCreateParam;
		
		Model.dataMaster = new BizData(options);

		//监视对象中的所有变动
		Model.dataMaster.deregRows = $scope.$watch('Model.dataMaster.rows',Model.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		////////////////////启动执行开始///////////////////////////////////////////////////
		//接收调用的参数
//		Model.params = $rootScope.$stateParams;  
		
		//刷新数据
//	    Model.dataMaster.refreshData();

		
		//////////////////////对话框使用开始//////////////////////////////////////
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
            Model.title = title;  
            Model.params = params;  
            console.log('Model.params-->>',Model.params);
            $scope.currentItem = Model.params.item;
            
            Model.dataMaster.filter = "ItemProper.itemID='"+$scope.currentItem.fID+"'";
            Model.dataMaster.refreshData();
        });  
		
		//刷新
		Model.refresh = function(){
			Model.dataMaster.refreshData();
		}
		
		//修改
		Model.edit = function(){
			Model.dataMaster.editData();
		}
		 
        Model.ok = function () {  
	    	Model.dataMaster.saveData();
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    
		//////////////////////对话框使用结束//////////////////////////////////////


		
		///////////////////子表按钮组开始///////////////////////////////////////////////////

		Model.insertItems = function(ids) {
			var options = {};
			options.itemID = $scope.currentItem.fID;
			options.ids = ids;
			
			$.ajax({
	            type: "POST",
	            async : false,
	            url: $rootScope.getProjectName() + "/inventory/itemProper/itemProperInsert",
	            data: JSON.stringify(options),// "{}",
	            contentType: "application/json; charset=utf-8",
	            dataType: "json",
	            success: function (data) {
	                // Play with returned data in JSON format
		        	console.log('Model.insertItems.data-->>',data);
		        	Model.dataMaster.refreshPageData();
	            }
	        });
		};

   		//增加
		Model.addrow = function(){
			var options = {};
			options.recordState = 'none';
			options.id = $scope.currentItem.enterpriseNo;

	        var modalInstance = $uibModal.open({  
	        	backdrop: 'static',
	        	size: 'lg',
	            templateUrl: $rootScope.getProjectName() + '/inventory/properEntrys/selectMulti',  
	            controller: 'properEntrysSelectMultiCtrl as Model',  
	            resolve: {
	                params: function () {  
	                    return angular.copy(options);  
	                }
                    ,title : function() { return '配置特征'; }
                    ,loadCtrl : $ocLazyLoad.load('js/inventory/properEntrys/selectMulti')
		        }
	        });  
	        //模态窗口打开之后执行的事件
	        modalInstance.opened.then(function(){
	        });  
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (result) {  
	        	//成功关闭窗口
	        	console.log('result-->>',result);
	        	var arr = result.data;
	        	var ids = [];
	        	for(var i=0;i<arr.length;i++){
	        		ids[i]=arr[i].fID;
	        	}
	        	Model.insertItems(ids);
	        }, function (reason) {  
	        	
	        });  

		};
		
		Model.delrow = function() {
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			
			var options = {};
			options.id = rowID;
			
			$.ajax({
	            type: "POST",
	            async : false,
	            url: $rootScope.getProjectName() + "/inventory/itemProper/itemProperDelete",
	            data: JSON.stringify(options),// "{}",
	            contentType: "application/json; charset=utf-8",
	            dataType: "json",
	            success: function (data) {
	                // Play with returned data in JSON format
		        	console.log('Model.delrow.data-->>',data);
		        	Model.dataMaster.refreshPageData();
	            }
	        });
		};
		
		///////////////////子表按钮组结束///////////////////////////////////////////////////


		
		
	    return Model;

	}	
	
});//define结束
	
